﻿import pygame
import sys
import math
import random
from pygame.locals import *

# 初始化 Pygame
pygame.init()

# 屏幕设置
WIDTH, HEIGHT = 1000, 700
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("小学常用公式原理展示")

# 颜色定义
BACKGROUND = (25, 30, 40)
TEXT_COLOR = (220, 220, 220)
HIGHLIGHT = (255, 215, 0)
FORMULA_COLOR = (100, 200, 255)
GRID_COLOR = (60, 70, 90)
SHAPE_COLOR = (70, 170, 220)
SHAPE_HIGHLIGHT = (100, 200, 250)
ANIMATION_COLOR = (255, 150, 50)
BUTTON_COLOR = (60, 100, 150)
BUTTON_HOVER = (80, 130, 200)

# 字体
title_font = pygame.font.SysFont("simhei", 48, bold=True)
subtitle_font = pygame.font.SysFont("simhei", 32)
text_font = pygame.font.SysFont("simhei", 24)
small_font = pygame.font.SysFont("simhei", 18)
formula_font = pygame.font.SysFont("simhei", 36, bold=True)

# 时钟
clock = pygame.time.Clock()
FPS = 60

# 公式列表
FORMULAS = [
    "勾股定理",
    "长方形面积",
    "圆的周长",
    "圆的面积",
    "三角形面积",
    "梯形面积",
    "乘法分配律",
    "分数加法",
    "分数乘法",
    "百分比"
]

# 按钮类
class Button:
    def __init__(self, x, y, width, height, text):
        self.rect = pygame.Rect(x, y, width, height)
        self.text = text
        self.color = BUTTON_COLOR
        self.hover = False
    
    def draw(self, surface):
        # 绘制按钮背景
        color = BUTTON_HOVER if self.hover else self.color
        pygame.draw.rect(surface, color, self.rect, border_radius=10)
        pygame.draw.rect(surface, HIGHLIGHT, self.rect, 2, border_radius=10)
        
        # 绘制按钮文本
        text = text_font.render(self.text, True, TEXT_COLOR)
        text_rect = text.get_rect(center=self.rect.center)
        surface.blit(text, text_rect)
    
    def is_clicked(self, pos):
        return self.rect.collidepoint(pos)
    
    def update_hover(self, pos):
        self.hover = self.rect.collidepoint(pos)

# 公式演示基类
class FormulaDemo:
    def __init__(self):
        self.stage = 0
        self.progress = 0
        self.max_stage = 3
        self.instructions = []
        self.formula_text = ""
    
    def next_stage(self):
        if self.stage < self.max_stage:
            self.stage += 1
            self.progress = 0
    
    def update(self):
        pass
    
    def draw(self, surface):
        # 绘制公式标题
        title = subtitle_font.render(self.formula_text, True, HIGHLIGHT)
        surface.blit(title, (WIDTH // 2 - title.get_width() // 2, 80))
        
        # 绘制当前阶段说明
        if self.stage < len(self.instructions):
            instruction = text_font.render(self.instructions[self.stage], True, TEXT_COLOR)
            surface.blit(instruction, (WIDTH // 2 - instruction.get_width() // 2, 120))
        
        # 绘制进度条
        if self.progress < 1.0:
            self.progress += 0.01
            if self.progress > 1.0:
                self.progress = 1.0
        
        # 绘制进度
        progress_text = small_font.render(f"进度: {int(self.progress * 100)}%", True, TEXT_COLOR)
        surface.blit(progress_text, (WIDTH - progress_text.get_width() - 20, HEIGHT - 30))

# 勾股定理演示
class PythagoreanTheoremDemo(FormulaDemo):
    def __init__(self):
        super().__init__()
        self.a = 200
        self.b = 150
        self.c = math.sqrt(self.a**2 + self.b**2)
        self.center_x = WIDTH // 2
        self.center_y = HEIGHT // 2
        self.triangle_points = []
        self.squares = []
        self.animation_pieces = []
        self.formula_text = "勾股定理: a² + b² = c²"
        self.instructions = [
            "1. 绘制直角三角形",
            "2. 在三条边上构建正方形",
            "3. 切割重组证明面积相等"
        ]
        self.create_shapes()
    
    def create_shapes(self):
        # 创建三角形
        self.triangle_points = [
            (self.center_x, self.center_y),  # 直角顶点
            (self.center_x + self.a, self.center_y),  # a边顶点
            (self.center_x, self.center_y - self.b)   # b边顶点
        ]
        
        # 创建正方形
        self.squares = [
            # a边正方形
            {"points": [
                (self.center_x, self.center_y),
                (self.center_x + self.a, self.center_y),
                (self.center_x + self.a, self.center_y + self.a),
                (self.center_x, self.center_y + self.a)
            ], "color": (220, 100, 100, 150)},
            
            # b边正方形
            {"points": [
                (self.center_x, self.center_y),
                (self.center_x, self.center_y - self.b),
                (self.center_x - self.b, self.center_y - self.b),
                (self.center_x - self.b, self.center_y)
            ], "color": (100, 220, 100, 150)},
            
            # c边正方形
            {"points": [
                (self.center_x + self.a, self.center_y),
                (self.center_x, self.center_y - self.b),
                (self.center_x - self.b, self.center_y - self.b + self.a),
                (self.center_x + self.a - self.b, self.center_y + self.a)
            ], "color": (100, 100, 220, 150)}
        ]
    
    def draw(self, surface):
        super().draw(surface)
        
        # 绘制三角形
        if self.stage >= 1:
            pygame.draw.polygon(surface, SHAPE_COLOR, self.triangle_points)
            pygame.draw.polygon(surface, HIGHLIGHT, self.triangle_points, 2)
            
            # 标记边长
            a_text = small_font.render(f"a = {self.a}", True, TEXT_COLOR)
            b_text = small_font.render(f"b = {self.b}", True, TEXT_COLOR)
            c_text = small_font.render(f"c = {self.c:.1f}", True, TEXT_COLOR)
            
            surface.blit(a_text, (self.center_x + self.a/2 - 20, self.center_y + 10))
            surface.blit(b_text, (self.center_x - 50, self.center_y - self.b/2))
            surface.blit(c_text, (self.center_x + self.a/2 - self.b/4, self.center_y - self.b/2 + self.a/4))
        
        # 绘制正方形
        if self.stage >= 2:
            for square in self.squares:
                # 创建半透明表面
                square_surface = pygame.Surface((WIDTH, HEIGHT), pygame.SRCALPHA)
                pygame.draw.polygon(square_surface, square["color"], square["points"])
                surface.blit(square_surface, (0, 0))
                
                pygame.draw.polygon(surface, HIGHLIGHT, square["points"], 2)
        
        # 绘制证明结果
        if self.stage == 3:
            result_y = HEIGHT - 150
            result_text = text_font.render(f"a² + b² = {self.a**2} + {self.b**2} = {self.a**2 + self.b**2}", True, HIGHLIGHT)
            surface.blit(result_text, (WIDTH // 2 - result_text.get_width() // 2, result_y))
            
            result_text2 = text_font.render(f"c² = ({self.c:.1f})² = {self.c**2:.0f}", True, HIGHLIGHT)
            surface.blit(result_text2, (WIDTH // 2 - result_text2.get_width() // 2, result_y + 40))

# 长方形面积演示
class RectangleAreaDemo(FormulaDemo):
    def __init__(self):
        super().__init__()
        self.length = 200
        self.width = 150
        self.unit_size = 20
        self.formula_text = "长方形面积 = 长 × 宽"
        self.instructions = [
            "1. 绘制长方形",
            "2. 分割成单位正方形",
            "3. 计算面积"
        ]
    
    def draw(self, surface):
        super().draw(surface)
        
        # 绘制长方形
        rect_x = WIDTH // 2 - self.length // 2
        rect_y = HEIGHT // 2 - self.width // 2
        rect = pygame.Rect(rect_x, rect_y, self.length, self.width)
        
        if self.stage >= 1:
            pygame.draw.rect(surface, SHAPE_COLOR, rect)
            pygame.draw.rect(surface, HIGHLIGHT, rect, 2)
            
            # 标记长和宽
            length_text = small_font.render(f"长 = {self.length}", True, TEXT_COLOR)
            width_text = small_font.render(f"宽 = {self.width}", True, TEXT_COLOR)
            
            surface.blit(length_text, (rect_x + self.length // 2 - 30, rect_y + self.width + 10))
            surface.blit(width_text, (rect_x - 50, rect_y + self.width // 2 - 10))
        
        # 绘制网格
        if self.stage >= 2:
            # 绘制水平线
            for y in range(rect_y, rect_y + self.width + 1, self.unit_size):
                pygame.draw.line(surface, GRID_COLOR, (rect_x, y), (rect_x + self.length, y), 1)
            
            # 绘制垂直线
            for x in range(rect_x, rect_x + self.length + 1, self.unit_size):
                pygame.draw.line(surface, GRID_COLOR, (x, rect_y), (x, rect_y + self.width), 1)
        
        # 绘制面积计算
        if self.stage == 3:
            # 计算单位正方形数量
            units_x = self.length // self.unit_size
            units_y = self.width // self.unit_size
            total_units = units_x * units_y
            
            # 绘制公式
            formula_y = HEIGHT - 150
            formula_text = text_font.render(f"面积 = 长 × 宽 = {self.length} × {self.width}", True, HIGHLIGHT)
            surface.blit(formula_text, (WIDTH // 2 - formula_text.get_width() // 2, formula_y))
            
            result_text = text_font.render(f"= {self.length * self.width}", True, HIGHLIGHT)
            surface.blit(result_text, (WIDTH // 2 - result_text.get_width() // 2, formula_y + 40))
            
            # 绘制单位正方形计数
            unit_text = small_font.render(f"每行有 {units_x} 个单位正方形，共有 {units_y} 行", True, TEXT_COLOR)
            surface.blit(unit_text, (WIDTH // 2 - unit_text.get_width() // 2, formula_y + 80))
            
            total_text = small_font.render(f"总单位正方形数 = {units_x} × {units_y} = {total_units}", True, TEXT_COLOR)
            surface.blit(total_text, (WIDTH // 2 - total_text.get_width() // 2, formula_y + 110))

# 圆的周长演示
class CircleCircumferenceDemo(FormulaDemo):
    def __init__(self):
        super().__init__()
        self.radius = 100
        self.center_x = WIDTH // 2
        self.center_y = HEIGHT // 2
        self.circumference = 2 * math.pi * self.radius
        self.formula_text = "圆的周长 = 2 × π × 半径"
        self.instructions = [
            "1. 绘制圆",
            "2. 展示直径和半径",
            "3. 展开圆周为直线"
        ]
        self.unrolled_progress = 0
    
    def update(self):
        super().update()
        if self.stage == 3 and self.unrolled_progress < 1.0:
            self.unrolled_progress += 0.01
    
    def draw(self, surface):
        super().draw(surface)
        
        # 绘制圆
        if self.stage >= 1:
            pygame.draw.circle(surface, SHAPE_COLOR, (self.center_x, self.center_y), self.radius)
            pygame.draw.circle(surface, HIGHLIGHT, (self.center_x, self.center_y), self.radius, 2)
            
            # 标记半径
            radius_text = small_font.render(f"半径 r = {self.radius}", True, TEXT_COLOR)
            surface.blit(radius_text, (self.center_x + self.radius + 10, self.center_y - 10))
            
            # 绘制半径线
            pygame.draw.line(surface, HIGHLIGHT, (self.center_x, self.center_y), 
                           (self.center_x + self.radius, self.center_y), 2)
        
        # 绘制直径
        if self.stage >= 2:
            pygame.draw.line(surface, HIGHLIGHT, 
                           (self.center_x - self.radius, self.center_y),
                           (self.center_x + self.radius, self.center_y), 2)
            
            diameter_text = small_font.render(f"直径 d = {2 * self.radius}", True, TEXT_COLOR)
            surface.blit(diameter_text, (self.center_x - diameter_text.get_width() // 2, self.center_y + 30))
        
        # 展开圆周为直线
        if self.stage == 3:
            # 绘制圆周展开线
            start_x = self.center_x - self.radius
            start_y = self.center_y + 200
            end_x = start_x + self.circumference * self.unrolled_progress
            
            pygame.draw.line(surface, HIGHLIGHT, (start_x, start_y), (end_x, start_y), 2)
            
            # 绘制标记
            if self.unrolled_progress > 0:
                pygame.draw.line(surface, HIGHLIGHT, (start_x, start_y - 10), (start_x, start_y + 10), 2)
                pygame.draw.line(surface, HIGHLIGHT, (end_x, start_y - 10), (end_x, start_y + 10), 2)
                
                length_text = small_font.render(f"{self.circumference * self.unrolled_progress:.1f}", True, TEXT_COLOR)
                surface.blit(length_text, ((start_x + end_x) // 2 - 20, start_y - 30))
            
            # 绘制公式
            formula_y = HEIGHT - 150
            formula_text = text_font.render(f"周长 = 2 × π × r = 2 × 3.14 × {self.radius}", True, HIGHLIGHT)
            surface.blit(formula_text, (WIDTH // 2 - formula_text.get_width() // 2, formula_y))
            
            result_text = text_font.render(f"≈ {self.circumference:.1f}", True, HIGHLIGHT)
            surface.blit(result_text, (WIDTH // 2 - result_text.get_width() // 2, formula_y + 40))

# 圆的面积演示
class CircleAreaDemo(FormulaDemo):
    def __init__(self):
        super().__init__()
        self.radius = 100
        self.center_x = WIDTH // 2
        self.center_y = HEIGHT // 2
        self.area = math.pi * self.radius ** 2
        self.formula_text = "圆的面积 = π × 半径²"
        self.instructions = [
            "1. 绘制圆",
            "2. 分割圆为扇形",
            "3. 重组为近似长方形"
        ]
        self.sectors = []
        self.create_sectors()
    
    def create_sectors(self):
        # 创建扇形
        num_sectors = 16
        angle_step = 2 * math.pi / num_sectors
        
        for i in range(num_sectors):
            start_angle = i * angle_step
            end_angle = (i + 1) * angle_step
            
            # 扇形点
            points = [(self.center_x, self.center_y)]
            for angle in [start_angle, end_angle]:
                x = self.center_x + self.radius * math.cos(angle)
                y = self.center_y + self.radius * math.sin(angle)
                points.append((x, y))
            
            self.sectors.append({
                "points": points,
                "color": (random.randint(100, 200), random.randint(100, 200), random.randint(100, 200)),
                "angle": (start_angle + end_angle) / 2
            })
    
    def draw(self, surface):
        super().draw(surface)
        
        # 绘制圆
        if self.stage >= 1:
            pygame.draw.circle(surface, SHAPE_COLOR, (self.center_x, self.center_y), self.radius)
            pygame.draw.circle(surface, HIGHLIGHT, (self.center_x, self.center_y), self.radius, 2)
            
            radius_text = small_font.render(f"半径 r = {self.radius}", True, TEXT_COLOR)
            surface.blit(radius_text, (self.center_x + self.radius + 10, self.center_y - 10))
        
        # 绘制扇形
        if self.stage >= 2:
            for sector in self.sectors:
                pygame.draw.polygon(surface, sector["color"], sector["points"])
                pygame.draw.polygon(surface, HIGHLIGHT, sector["points"], 1)
        
        # 绘制重组为长方形
        if self.stage == 3:
            # 计算长方形尺寸
            rect_width = math.pi * self.radius
            rect_height = self.radius
            rect_x = WIDTH // 2 - rect_width // 2
            rect_y = HEIGHT // 2 + 100
            
            # 绘制长方形
            pygame.draw.rect(surface, SHAPE_COLOR, (rect_x, rect_y, rect_width, rect_height))
            pygame.draw.rect(surface, HIGHLIGHT, (rect_x, rect_y, rect_width, rect_height), 2)
            
            # 标记尺寸
            width_text = small_font.render(f"宽 ≈ π × r = {math.pi:.2f} × {self.radius}", True, TEXT_COLOR)
            height_text = small_font.render(f"高 = r = {self.radius}", True, TEXT_COLOR)
            
            surface.blit(width_text, (rect_x + rect_width // 2 - 80, rect_y + rect_height + 20))
            surface.blit(height_text, (rect_x - 50, rect_y + rect_height // 2 - 10))
            
            # 绘制公式
            formula_y = HEIGHT - 150
            formula_text = text_font.render(f"面积 = π × r² = {math.pi:.2f} × {self.radius}²", True, HIGHLIGHT)
            surface.blit(formula_text, (WIDTH // 2 - formula_text.get_width() // 2, formula_y))
            
            result_text = text_font.render(f"≈ {self.area:.1f}", True, HIGHLIGHT)
            surface.blit(result_text, (WIDTH // 2 - result_text.get_width() // 2, formula_y + 40))

# 三角形面积演示
class TriangleAreaDemo(FormulaDemo):
    def __init__(self):
        super().__init__()
        self.base = 200
        self.height = 150
        self.center_x = WIDTH // 2
        self.center_y = HEIGHT // 2
        self.area = 0.5 * self.base * self.height
        self.formula_text = "三角形面积 = 底 × 高 ÷ 2"
        self.instructions = [
            "1. 绘制三角形",
            "2. 展示底和高",
            "3. 转化为长方形"
        ]
    
    def draw(self, surface):
        super().draw(surface)
        
        # 三角形顶点
        points = [
            (self.center_x - self.base // 2, self.center_y + self.height // 2),  # 左下
            (self.center_x + self.base // 2, self.center_y + self.height // 2),  # 右下
            (self.center_x, self.center_y - self.height // 2)  # 顶点
        ]
        
        # 绘制三角形
        if self.stage >= 1:
            pygame.draw.polygon(surface, SHAPE_COLOR, points)
            pygame.draw.polygon(surface, HIGHLIGHT, points, 2)
            
            # 标记底边
            base_text = small_font.render(f"底 = {self.base}", True, TEXT_COLOR)
            surface.blit(base_text, (self.center_x - base_text.get_width() // 2, self.center_y + self.height // 2 + 20))
            
            # 标记高
            height_text = small_font.render(f"高 = {self.height}", True, TEXT_COLOR)
            surface.blit(height_text, (self.center_x + self.base // 2 + 10, self.center_y))
        
        # 绘制高线
        if self.stage >= 2:
            pygame.draw.line(surface, HIGHLIGHT, 
                           (self.center_x, self.center_y + self.height // 2),
                           (self.center_x, self.center_y - self.height // 2), 2)
        
        # 转化为长方形
        if self.stage == 3:
            # 绘制长方形
            rect_x = self.center_x - self.base // 2
            rect_y = self.center_y - self.height // 2
            pygame.draw.rect(surface, SHAPE_COLOR, (rect_x, rect_y, self.base, self.height))
            pygame.draw.rect(surface, HIGHLIGHT, (rect_x, rect_y, self.base, self.height), 2)
            
            # 绘制对角线
            pygame.draw.line(surface, HIGHLIGHT, (rect_x, rect_y), (rect_x + self.base, rect_y + self.height), 2)
            
            # 标记尺寸
            base_text = small_font.render(f"长 = 底 = {self.base}", True, TEXT_COLOR)
            height_text = small_font.render(f"宽 = 高 = {self.height}", True, TEXT_COLOR)
            
            surface.blit(base_text, (rect_x + self.base // 2 - 50, rect_y + self.height + 20))
            surface.blit(height_text, (rect_x - 50, rect_y + self.height // 2 - 10))
            
            # 绘制公式
            formula_y = HEIGHT - 150
            formula_text = text_font.render(f"三角形面积 = 长方形面积 ÷ 2", True, HIGHLIGHT)
            surface.blit(formula_text, (WIDTH // 2 - formula_text.get_width() // 2, formula_y))
            
            result_text = text_font.render(f"= ({self.base} × {self.height}) ÷ 2 = {self.area}", True, HIGHLIGHT)
            surface.blit(result_text, (WIDTH // 2 - result_text.get_width() // 2, formula_y + 40))

# 梯形面积演示
class TrapezoidAreaDemo(FormulaDemo):
    def __init__(self):
        super().__init__()
        self.top_base = 150
        self.bottom_base = 200
        self.height = 120
        self.center_x = WIDTH // 2
        self.center_y = HEIGHT // 2
        self.area = (self.top_base + self.bottom_base) * self.height / 2
        self.formula_text = "梯形面积 = (上底 + 下底) × 高 ÷ 2"
        self.instructions = [
            "1. 绘制梯形",
            "2. 展示上底、下底和高",
            "3. 转化为平行四边形"
        ]
    
    def draw(self, surface):
        super().draw(surface)
        
        # 梯形顶点
        points = [
            (self.center_x - self.bottom_base // 2, self.center_y + self.height // 2),  # 左下
            (self.center_x + self.bottom_base // 2, self.center_y + self.height // 2),  # 右下
            (self.center_x + self.top_base // 2, self.center_y - self.height // 2),  # 右上
            (self.center_x - self.top_base // 2, self.center_y - self.height // 2)  # 左上
        ]
        
        # 绘制梯形
        if self.stage >= 1:
            pygame.draw.polygon(surface, SHAPE_COLOR, points)
            pygame.draw.polygon(surface, HIGHLIGHT, points, 2)
            
            # 标记上底和下底
            top_base_text = small_font.render(f"上底 = {self.top_base}", True, TEXT_COLOR)
            bottom_base_text = small_font.render(f"下底 = {self.bottom_base}", True, TEXT_COLOR)
            
            surface.blit(top_base_text, (self.center_x - top_base_text.get_width() // 2, self.center_y - self.height // 2 - 30))
            surface.blit(bottom_base_text, (self.center_x - bottom_base_text.get_width() // 2, self.center_y + self.height // 2 + 20))
            
            # 标记高
            height_text = small_font.render(f"高 = {self.height}", True, TEXT_COLOR)
            surface.blit(height_text, (self.center_x + self.bottom_base // 2 + 10, self.center_y))
        
        # 绘制高线
        if self.stage >= 2:
            pygame.draw.line(surface, HIGHLIGHT, 
                           (self.center_x, self.center_y + self.height // 2),
                           (self.center_x, self.center_y - self.height // 2), 2)
        
        # 转化为平行四边形
        if self.stage == 3:
            # 绘制平行四边形
            para_x = self.center_x - self.bottom_base // 2
            para_y = self.center_y - self.height // 2
            para_points = [
                (para_x, para_y),
                (para_x + self.bottom_base, para_y),
                (para_x + self.bottom_base + (self.top_base - self.bottom_base) // 2, para_y + self.height),
                (para_x + (self.top_base - self.bottom_base) // 2, para_y + self.height)
            ]
            
            pygame.draw.polygon(surface, SHAPE_COLOR, para_points)
            pygame.draw.polygon(surface, HIGHLIGHT, para_points, 2)
            
            # 绘制公式
            formula_y = HEIGHT - 150
            formula_text = text_font.render(f"梯形面积 = 平行四边形面积 ÷ 2", True, HIGHLIGHT)
            surface.blit(formula_text, (WIDTH // 2 - formula_text.get_width() // 2, formula_y))
            
            result_text = text_font.render(f"= ({self.top_base} + {self.bottom_base}) × {self.height} ÷ 2 = {self.area}", True, HIGHLIGHT)
            surface.blit(result_text, (WIDTH // 2 - result_text.get_width() // 2, formula_y + 40))

# 主菜单类
class MainMenu:
    def __init__(self):
        self.buttons = []
        self.create_buttons()
        self.current_demo = None
    
    def create_buttons(self):
        button_width = 200
        button_height = 50
        button_margin = 20
        start_x = WIDTH // 2 - button_width // 2
        start_y = 200
        
        for i, formula in enumerate(FORMULAS):
            y = start_y + i * (button_height + button_margin)
            self.buttons.append(Button(start_x, y, button_width, button_height, formula))
    
    def handle_click(self, pos):
        for i, button in enumerate(self.buttons):
            if button.is_clicked(pos):
                return FORMULAS[i]
        return None
    
    def update_hover(self, pos):
        for button in self.buttons:
            button.update_hover(pos)
    
    def draw(self, surface):
        # 绘制背景
        surface.fill(BACKGROUND)
        
        # 绘制标题
        title = title_font.render("小学常用公式原理展示", True, TEXT_COLOR)
        surface.blit(title, (WIDTH // 2 - title.get_width() // 2, 50))
        
        subtitle = subtitle_font.render("选择要查看的公式", True, HIGHLIGHT)
        surface.blit(subtitle, (WIDTH // 2 - subtitle.get_width() // 2, 120))
        
        # 绘制按钮
        for button in self.buttons:
            button.draw(surface)
        
        # 绘制说明
        instruction = text_font.render("点击公式名称查看原理演示", True, TEXT_COLOR)
        surface.blit(instruction, (WIDTH // 2 - instruction.get_width() // 2, HEIGHT - 100))

# 主函数
def main():
    menu = MainMenu()
    current_demo = None
    running = True
    
    while running:
        mouse_pos = pygame.mouse.get_pos()
        
        for event in pygame.event.get():
            if event.type == QUIT:
                running = False
            elif event.type == MOUSEBUTTONDOWN:
                if event.button == 1:  # 左键点击
                    if current_demo:
                        current_demo.next_stage()
                    else:
                        formula = menu.handle_click(event.pos)
                        if formula == "勾股定理":
                            current_demo = PythagoreanTheoremDemo()
                        elif formula == "长方形面积":
                            current_demo = RectangleAreaDemo()
                        elif formula == "圆的周长":
                            current_demo = CircleCircumferenceDemo()
                        elif formula == "圆的面积":
                            current_demo = CircleAreaDemo()
                        elif formula == "三角形面积":
                            current_demo = TriangleAreaDemo()
                        elif formula == "梯形面积":
                            current_demo = TrapezoidAreaDemo()
            elif event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    if current_demo:
                        current_demo = None
                    else:
                        running = False
                elif event.key == K_SPACE and current_demo:
                    current_demo.next_stage()
        
        # 更新
        if current_demo:
            current_demo.update()
        else:
            menu.update_hover(mouse_pos)
        
        # 绘制
        if current_demo:
            current_demo.draw(screen)
        else:
            menu.draw(screen)
        
        # 绘制返回按钮
        if current_demo:
            back_text = text_font.render("按ESC键返回主菜单", True, TEXT_COLOR)
            screen.blit(back_text, (WIDTH - back_text.get_width() - 20, 20))
        
        pygame.display.flip()
        clock.tick(FPS)
    
    pygame.quit()
    sys.exit()

if __name__ == "__main__":
    main()